{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# An implementation of the Ackermann function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "logtalk"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[1mtrue"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%load ack.lgt\n",
    "\n",
    ":- object(ack).\n",
    "\n",
    ":- info([\n",
    "\tversion is 1:0:0,\n",
    "\tauthor is 'Paulo Moura',\n",
    "\tdate is 2008-3-31,\n",
    "\tcomment is 'Ackermann function (general recursive function).'\n",
    "]).\n",
    "\n",
    ":- public(ack/3).\n",
    ":- mode(ack(+integer, +integer, -integer), one).\n",
    ":- info(ack/3, [\n",
    "\tcomment is 'Ackermann function.',\n",
    "\targnames is ['M', 'N', 'V']\n",
    "]).\n",
    "\n",
    "ack(0, N, V) :-\n",
    "\t!,\n",
    "\tV is N + 1.\n",
    "ack(M, 0, V) :-\n",
    "\t!,\n",
    "\tM2 is M - 1,\n",
    "\tack(M2, 1, V).\n",
    "ack(M, N, V) :-\n",
    "\tM2 is M - 1,\n",
    "\tN2 is N - 1,\n",
    "\tack(M, N2, V2),\n",
    "\tack(M2, V2, V).\n",
    "\n",
    ":- end_object."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sample query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "logtalk"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[1mV = 11"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ack::ack(2, 4, V)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Logtalk",
   "language": "logtalk",
   "name": "logtalk_kernel"
  },
  "language_info": {
   "codemirror_mode": "logtalk",
   "file_extension": ".lgt",
   "mimetype": "text/x-logtalk",
   "name": "Logtalk"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
